import 'package:flutter/material.dart';
import 'package:aio/aio.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/wroks/sub_payfree_good_return.dart';
import 'package:app/src/http/bean/wroks/sub_payfree_return.dart';
import 'package:app/src/manager/audit_manager.dart';
import 'package:app/src/presenter/review_presenter.dart';
import 'package:app/src/ui/app_ui.dart';
import 'package:app/src/ui/dialog/dailog.dart';
import 'package:app/src/ui/route/works/public/acom/ui_payfreegoodreturn.dart';
import 'package:app/src/ui/route/works/public/acom/ui_payfreegoodreturn_approved.dart';
import 'package:app/src/ui/route/works/public/acom/ui_payfreegoodreturn_patch.dart';
import 'package:app/src/ui/route/works/public/image/ui_image.dart';
import 'package:app/src/ui/route/works/public/items/ui_attachment.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_contractInfo.dart';
import 'package:app/src/ui/route/works/public/items/ui_customersInfo.dart';
import 'package:app/src/ui/route/works/public/items/ui_new_customer.dart';
import 'package:app/src/ui/route/works/public/items/ui_node_list.dart';
import 'package:app/src/ui/route/works/public/items/ui_note.dart';
import 'package:app/src/ui/route/works/public/acom/ui_order_item_info.dart';
import 'package:app/src/ui/route/works/public/items/ui_payfree_return.dart';
import 'package:app/src/ui/route/works/public/items/ui_payment.dart';
import 'package:app/src/ui/route/works/public/acom/ui_process_info.dart';
import 'package:app/src/ui/route/works/public/items/ui_process_list.dart';
import 'package:app/src/ui/route/works/public/acom/ui_product.dart';
import 'package:app/src/ui/route/works/public/items/ui_product_info.dart';
import 'package:app/src/ui/route/works/public/items/ui_single.dart';
import 'package:app/src/ui/route/works/public/items/ui_specialclauses.dart';
import 'package:app/src/ui/route/works/public/review_details_ui.dart';
import 'package:app/src/ui/route/works/public/review_submitted.dart';

///退货审核详情
class PayFreeGoodReturnDetailsRoute extends StatefulWidget {
  PayFreeGoodReturnDetailsRoute();

  @override
  _State createState() => _State();
}

class _State extends State<PayFreeGoodReturnDetailsRoute> {
  _Presenter presenter = _Presenter();

  @override
  void initState() {
    super.initState();
    presenter.getContractapplygetinfoWithDelay(context); //获取审核详情数据
  }

  @override
  Widget build(BuildContext context) {
    return ReviewDetailsUi.build(context, presenter, _body);
  }

  Widget _body(BuildContext context) {
    if (presenter?.mDatas == null) {
      return AppUi.getEmptyView();
    }
    List<Widget> children = [];
    children.add(UiSingle.build(
        context,
        presenter?.mDatas?.PayFreeReturn?.ReviewTypePosition,
        presenter?.mOldDatas?.PayFreeReturn?.ReviewTypePosition,
        title: AppStrings.of(context).ReviewTypePosition)); //当前所在位置
    children.add(UiPayFreeReturnInfo.build(context,
        presenter?.mDatas?.PayFreeReturn, presenter?.mOldDatas?.PayFreeReturn,
        title: AppStrings.of(context).PayFreeReturnInfo)); //退货信息
    children.add(UiNote.build(context, presenter?.mDatas?.PayFreeReturn?.Intro,
        presenter?.mOldDatas?.PayFreeReturn?.Intro,
        title: AppStrings.of(context).Intro3)); //退货说明
    children.add(UiPayFreeGoodReturn.build(
        context,
        presenter?.mDatas?.PayFreeReturnList,
        presenter?.mDatas?.PayFreeReturn?.isRackgoes() ?? false)); //退货审核明细
    var data = presenter?.mDatas?.PayFreeReturn;
    //fixme 退货补片，才有入库仓库和检验员
    if (data?.isReturnsPatch() ?? false) {
      //入库仓库
      var StorehouseList = data?.StorehouseList;
      if (StringUtils.isNotEmpty(StorehouseList)) {
        if (data?.Storehouse == null) {
          data?.Storehouse = StorehouseList![0];
        }
        var builder = StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
          var Storehouse = data?.Storehouse;
          return UiSingle.build(
              context,
              presenter?.mDatas?.PayFreeReturn?.Storehouse?.StorehouseName,
              presenter?.mOldDatas?.PayFreeReturn?.Storehouse?.StorehouseName,
              title: AppStrings.of(context).StorehouseList,
              isBlue: true, onTap: () {
            AppDialog.showDialogWithRollerPicker(context,
                    datas: StorehouseList, data: Storehouse)
                ?.then((value) {
              if (value != null) {
                setState(() {
                  presenter?.mDatas?.PayFreeReturn?.Storehouse = value;
                });
              }
            });
          });
        });
        children.add(builder);
      }
      //检验员
      var CheckerList = data?.CheckerList;
      if (StringUtils.isNotEmpty(CheckerList)) {
        if (data?.Checker == null) {
          data?.Checker = CheckerList![0];
        }
        var builder = StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
          var check = data?.Checker;
          return UiSingle.build(
              context,
              presenter?.mDatas?.PayFreeReturn?.Checker?.PersonName,
              presenter?.mOldDatas?.PayFreeReturn?.Checker?.PersonName,
              title: AppStrings.of(context).CheckerList,
              isBlue: true, onTap: () {
            AppDialog.showDialogWithRollerPicker(context,
                    datas: CheckerList, data: check)
                ?.then((value) {
              if (value != null) {
                setState(() {
                  presenter?.mDatas?.PayFreeReturn?.Checker = value;
                });
              }
            });
          });
        });
        children.add(builder);
      }
    }
    children.add(UiPayFreeGoodReturnPatch.build(
      context,
      presenter?.mDatas?.PayFreeGoodReturnList,
    )); //退货补片明细
    children.add(UiPayFreeGoodReturnApproved.build(
      context,
      presenter?.mDatas?.PayFreeReturnInfoList,
    )); //退货审核明细
    children.add(UiImage.build(context, presenter?.mDatas?.PicList)); //图片证据
    children.add(UiAttachment.build(context, presenter?.mDatas?.FileList)); //附件
    children.add(UiNoteList.build(context, presenter?.mDatas?.NodeList)); //审核日志
    return Column(
      children: [
        Expanded(child: UiBase.listView(context, children)),
        Visibility(
            child: ReviewSubmittedStateful(
              presenter?.mDatas,
              hasEnd: false,
              callbackWithT: (subData) {
                //fixme 退货审核，责任工序，责任班组，责任人
                if (subData.PayFreeReturnList == null) {
                  subData.PayFreeReturnList = [];
                }
                subData.PayFreeReturnList?.clear(); //清空一下。
                presenter?.mDatas?.PayFreeReturnList?.mList?.forEach((element) {
                  SubPayFreeReturn value = SubPayFreeReturn();
                  value.isRackgoes = element.isRackgoes;
                  value.ProcessId = element.ProcessId;
                  value.GroupId = element.GroupId;
                  value.PersonId = element.PersonId;
                  value.Details = element?.Details;
                  subData.PayFreeReturnList?.add(value);
                });
                //fixme 报废数量，拆片数据,退库数量,操作班组，可拆片数量
                if (subData.PayFreeGoodReturnList == null) {
                  subData.PayFreeGoodReturnList = [];
                }
                subData?.PayFreeGoodReturnList?.clear();
                presenter?.mDatas?.PayFreeGoodReturnList?.forEach((element) {
                  SubPayFreeGoodReturn sub = SubPayFreeGoodReturn();
                  sub.WasteNum = element.WasteNum; //报废数量
                  sub.CPNum = element.CPNum; //拆片数量
                  sub.BackNum = element.BackNum; //退库数量
                  sub.Group = element.GroupId; //操作班组
                  sub.CPList = []; //可拆片数量
                  element?.mList?.forEach((element) {
                    sub.CPList?.add(element.CPNum);
                  });
                  subData?.PayFreeGoodReturnList?.add(sub);
                });
                //fixme 入库仓库，检验员
                subData?.InStoreHouse = presenter
                    ?.mDatas?.PayFreeReturn?.Storehouse?.StorehouseId; //入库仓库
                subData?.Checker =
                    presenter?.mDatas?.PayFreeReturn?.Checker?.PersonId; //检验员
              },
            ),
            visible: presenter?.mDatas != null)
      ],
    );
  }
}

class _Presenter extends ReviewPresenter {}
